package com.tianlang.jfinal.run;

import java.io.File;

import org.beetl.core.GroupTemplate;
import org.beetl.ext.jfinal3.JFinal3BeetlRenderFactory;

import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.core.JFinal;
import com.jfinal.kit.PathKit;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.CaseInsensitiveContainerFactory;
import com.jfinal.plugin.activerecord.dialect.MysqlDialect;
import com.jfinal.plugin.activerecord.dialect.OracleDialect;
import com.jfinal.plugin.activerecord.dialect.PostgreSqlDialect;
import com.jfinal.plugin.activerecord.dialect.Sqlite3Dialect;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.template.Engine;
import com.tianlang.annotation.AutoBindModels;
import com.tianlang.annotation.AutoBindRoutes;
import com.tianlang.beetl.tag.AuthorityTag;
import com.tianlang.handler.GlobalInterceptor;

/**
 * Jfinal API 引导式配置
 * 
 * @author mcp
 *
 */
public class JfinalConfig extends JFinalConfig {

	// private static Logger log = Logger.getLogger(JfinalConfig.class);

	/**
	 * 运行此 main 方法可以启动项目 说明： 1. linux 下非root账户运行端口要>1024 2. idea
	 * 中运行记得加上当前的module名称
	 */
	public static void main(String[] args) {
		JFinal.start("WebContent", 89, "/", 5);
	}

	/**
	 * 常量配置
	 */
	@Override
	public void configConstant(Constants me) {
		// 数据库配置文件加载
		// String classPath = PathKit.getRootClassPath() + File.separator;
		PropKit.use("init.properties");
		// 设置开发模式
		me.setDevMode(false);
		// 设置beetl模版
		JFinal3BeetlRenderFactory rf = new JFinal3BeetlRenderFactory();
		rf.config();
		me.setRenderFactory(rf);
		// 获取GroupTemplate ,可以设置共享变量等操作
		GroupTemplate groupTemplate = rf.groupTemplate;
		// 注册全局自定义权限标签
		groupTemplate.registerTag("wcs", AuthorityTag.class);
	}

	/**
	 * 路由映射绑定
	 */
	@Override
	public void configRoute(Routes me) {
		// 自动绑定
		// 1.如果没用加入注解，必须以Controller结尾,自动截取前半部分为key
		// 2.加入ControllerBind的 获取 key
		me.add(new AutoBindRoutes("/WEB-INF/view"));
	}

	/**
	 * 插件添加
	 */
	@Override
	public void configPlugin(Plugins me) {
		loadPropertyFile("init.properties");
		DruidPlugin druidPlugin = new DruidPlugin(PropKit.get("jdbc.url"), PropKit.get("jdbc.username"),
				PropKit.get("jdbc.password"), PropKit.get("jdbc.driverClass"));
		me.add(druidPlugin);
		druidPlugin.setInitialSize(PropKit.getInt("jdbc.initialPoolSize"));
		druidPlugin.setMaxActive(PropKit.getInt("jdbc.maxPoolSize"));
		druidPlugin.setMinIdle(PropKit.getInt("jdbc.minPoolSize"));
		druidPlugin.setMaxWait(PropKit.getInt("jdbc.maxIdleTime"));
		druidPlugin.setTimeBetweenEvictionRunsMillis(PropKit.getInt("jdbc.timeBetweenEvictionRunsMillis"));
		druidPlugin.setTestWhileIdle(PropKit.getBoolean("jdbc.testWhileIdle"));
		druidPlugin.setTestOnBorrow(PropKit.getBoolean("jdbc.testOnBorrow"));
		// 配置ActiveRecord插件
		ActiveRecordPlugin arp = new ActiveRecordPlugin(druidPlugin);
		me.add(arp);
		// 设置开发模式
		arp.setDevMode(false);
		// 是否显示SQL
		arp.setShowSql(true);
		// 配置属性名(字段名)大小写不敏感容器工厂
		arp.setContainerFactory(new CaseInsensitiveContainerFactory());
		// 指定方言
		String dbType = PropKit.get("jdbc.type");
		if ("mysql".equals(dbType)) {
			arp.setDialect(new MysqlDialect());
		} else if ("oracle".equals(dbType)) {
			arp.setDialect(new OracleDialect());
		} else if ("sqlite".equals(dbType)) {
			arp.setDialect(new Sqlite3Dialect());
		} else if ("postgresql".equals(dbType)) {
			arp.setDialect(new PostgreSqlDialect());
		}
		// jfinal sql模版加载
		arp.setBaseSqlTemplatePath(PathKit.getRootClassPath() + File.separator + "com" + File.separator + "tianlang"
				+ File.separator + "sql");
		arp.addSqlTemplate("all.sql");
		new AutoBindModels(arp);
	}

	@Override
	public void configEngine(Engine me) {

	}

	/**
	 * 全局过滤器
	 */
	@Override
	public void configInterceptor(Interceptors me) {
		me.add(new GlobalInterceptor());
	}

	@Override
	public void configHandler(Handlers me) {
		// me.add(new GlobalHandler());
	}
}
